package com.travel.server.manager.impl.command.message;

import com.travel.common.util.CollectionUtil;
import com.travel.server.manager.dto.request.EmptyRequest;
import com.travel.server.manager.dto.response.message.QueryMessageTemplateResponse;
import com.travel.server.manager.dto.response.message.QueryMessageTemplateResponse.TemplateItem;
import com.travel.server.manager.impl.command.AbstractCommand;
import com.travel.server.manager.impl.dal.MessageDal;
import com.travel.server.manager.impl.dal.MessageDal.MessageParam;
import com.travel.server.manager.impl.enums.MessageExamStateEnum;
import com.travel.server.manager.impl.enums.MessageTemplateEnum;
import com.travel.server.manager.model.MessageTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: LiZhenhui
 * @Date: 2018/11/5 18:22
 * @Desc:
 */
@Component
@Slf4j
public class QueryMessageTemplateCommand extends AbstractCommand<EmptyRequest, QueryMessageTemplateResponse> {

    @Autowired
    private MessageDal messageDal;

    @Override
    protected void checkInput(EmptyRequest request) {

    }

    @Override
    public QueryMessageTemplateResponse handle(EmptyRequest request) {
        QueryMessageTemplateResponse response = new QueryMessageTemplateResponse();

        MessageParam param = new MessageParam();
        param.setExamState(MessageExamStateEnum.EXAM_SUCCESS.getState());
        List<MessageTemplate> list = messageDal.selectByExample(param);

        if(!CollectionUtil.isEmpty(list)){
            List<TemplateItem> mailList = new ArrayList<>();
            List<TemplateItem> msgList = new ArrayList<>();

            for(MessageTemplate template : list){
                TemplateItem item = makeTemplateItem(template);
                if(MessageTemplateEnum.MSG.getType().equals(template.getMessageType())){
                    msgList.add(item);
                }else if(MessageTemplateEnum.MAIL.getType().equals(template.getMessageType())){
                    mailList.add(item);
                }
            }

            response.setMailList(mailList);
            response.setMsgList(msgList);
        }

        return response;
    }

    private TemplateItem makeTemplateItem(MessageTemplate template){
        TemplateItem item = new TemplateItem();
        item.setTitle(template.getMessageTitle());
        item.setId(template.getId());
        return item;
    }
}
